<template>
	<view :class="identity==2?'container':'container1'">
		<view class="header">
			<view class="order-top">
				<view  class="top-left-box">
						<view class="top-left" @tap="personal">
							<image class="person" src="/static/images/person.png"></image>
							<text class="linehei" v-if="identity!=5">{{salesmanNo}}</text>
						</view>
						<!-- 扫码 -->
						<!-- <image v-if="identity!=2" class="sys" @tap="scanCode" src="/static/images/sys.png" mode=""></image> -->
						
						<view class="top-right" @tap="search">
							<image class="search" src="/static/images/search.png" mode=""></image>
							<input type="text" value=""  disabled="true" class="search-input" placeholder="订单编号搜索" placeholder-class="sty" />
							
						</view>
				</view>
				<view class="top-right-box ">
					<!-- 扫码 -->
					<image v-if="identity!=2" class="sys" @tap="scanCode" src="/static/images/sys.png" mode=""></image>
					
					<!-- 消息 -->
					<view class="message-img " @tap="skipToMessage">
						<image class="message-img-icon" src="/static/images/message.png"  mode=""></image>
						<view class=" info-num" v-if="messagenumber!=0">{{messagenumber}}</view>
					</view>
					
				</view>
			</view>
			<!-- 经理有 -->
			<view class="card" v-if="identity==2">
				<view class="card-list" @tap="jump('已预付',1)">
					<!--  -->
					<view class="num">{{orderResume.prePayOrders}}</view>
					<view class="dec">已预付</view>
				</view>
				<!--  -->
				<view class="card-list" @tap="jump('货到付款',2)">
					<view class="num">{{orderResume.collectOrders}}</view>
					<view class="dec">货到付款</view>
				</view>
				<!--  -->
				<view class="card-list"  @tap="jump('赊账',3)">
					<view class="num">{{orderResume.onCreditOrders}}</view>
					<view class="dec">赊账中</view>
				</view>
				
				<view class="card-list"  @tap="jump('待上传凭证',4)">
					<view class="num">{{orderResume.onTransferPayOrder}}</view>
					<view class="dec">待上传凭证</view>
				</view>
			</view>
			<view class="table">
				<scroll-view class="scroll" scroll-x="true" show-scrollbar="false">
					<view class="table-box ">
						<view :class="'table-list '+(active==item.index?'active':'')" v-for="(item,index) in tableList" :key="index" @tap="table(item.index)">
							{{item.tableName}}
						</view>
					</view>
				</scroll-view>
				
				
			</view>
		</view>
		<!-- '0全部','1待处理','2待指派','3待配送','4待收货','5已收货','6已取消' -->
		<!-- 订单列表 --> 
		<order-list :list="orderList" :dx="dx" ></order-list>
		<view class="js-drop" v-if="sj">
			  <view class="js-box" style="background: url(../../static/images/upgrade.png) no-repeat center; background-size: 100% 100%; ">
				  <view class="version">V{{version}}</view>
				  
				  <image class="js-close" v-if="!constraint" @tap="sjClose" src="../../static/images/sjclose.png"></image>
				  <scroll-view class="js-scroll" scroll-y="true" >
				  	<view class="js-list" v-for="(item,index) in bugList">
				  		 {{item}}
				  	</view>
				  </scroll-view>
				  
				  <view class="btn" @tap="upgrade">立即更新</view>
			  </view>
		  </view> 
</view>
		
</template>

<script>
	const util = require("../../utils/util.js");
	//获取应用实例
	const api = require("../../config/api.js"); 
	import orderList from "../../components/orderList/orderList"; 
	
	export default {
		data() {
			return {
				tableList:[],
				active:-1,
				// 拒绝理由
				reasonRefusal:'',
				// 订单遮罩控制
				orderDrop:false,
				// 身份
				identity:0,
				// 订单列表
				orderList:[],
				//业务员编号
				salesmanNo:'',
				// 业务员概要
				orderResume:{},
				// 页数
				page:1,
				// 没有更多数据
				dx:false,
				sj:false,
				bugList:'',
				version:'',
				constraint:false,
				downloadUrl:'',
				messagenumber:0
			}
		},
		components: {
		  orderList
		},
		onLoad(){
			
			//#ifdef APP-PLUS
			// 检测升级
			  util.request(api.appversionCheck, {}, "POST").then((res)=>{
						console.log(res)
						// 强制升级
						if(res.error==90){
							console.log('升级')
							 this.sj = true
							 this.downloadUrl = res.data.downloadUrl;
								 // 判断是否强子升级
							 this.constraint=true;
							 this.bugList = res.data.remark.split('|');
							 this.version = res.data.version;
							  console.log(this.bugList)
						}else if(res.error==91){
							// 选择升级 
							 this.sj = true
							 this.downloadUrl = res.data.downloadUrl;
							 this.bugList = res.data.remark.split('|');
								  this.version = res.data.version;
							 console.log(this.bugList )
						}
						
						
						if(res.error==0){
							// 判断是否登录
							var isLogin = uni.getStorageSync('isLogin');
							if(uni.getStorageSync('identity')==3){
								uni.reLaunch({
									url: '/pages/generalManager/generalManager'
								})
							}
							if(!isLogin){
								uni.redirectTo({
									url: '/pages/login/login'
								})
							}
						}
			  })
			 //#endif  
			 
			// 获取身份
			// 存入人员id（0:B端用户 1:C端用户 2:业务员 3:经理 4:司机 5:仓管）
			this.identity = uni.getStorageSync('identity')
			
			
			if(this.identity==2){
				this.tableList =[
					{
						tableName:'全部',
						index:''
					},
					{
						tableName:'待处理',
						index:-1
					},
					{
						tableName:'待审核',
						index:1
					},
					{
						tableName:'待配送',
						index:3
					},
					{
						tableName:'待收货',
						index:4
					},
					{
						tableName:'已收货',
						index:5
					},
					{
						tableName:'已取消',
						index:6
					}
					]
					
			}else if(this.identity==4){
				this.active = 2
				this.tableList =[
					{
						tableName:'全部',
						index:''
					},
					{
						tableName:'待接单',
						index:2
					},
					{
						tableName:'待配送',
						index:3
					},
					
					{
						tableName:'待收货',
						index:4
					},
					{
						tableName:'已收货',
						index:5
					}
					]
				
			}else if(this.identity==5){
				this.tableList =[
					{
						tableName:'全部',
						index:''
					},
					{
						tableName:'待处理',
						index:-1
					},
					{
						tableName:'待接单',
						index:2
					},
					{
						tableName:'待配送',
						index:3
					},
					{
						tableName:'待收货',
						index:4
					},
					{
						tableName:'已收货',
						index:5
					},
					{
						tableName:'已取消',
						index:6
					}
					]
			}
			
			 
				
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page=1
		    this.getOrderList()
			if(this.identity == 2){
				console.log('---------------------------')
				this.getOrderResume()
			
			}
			// 请求消息列表
			this.getInfo();
		},
		// 上拉加载
		onReachBottom(){
			uni.showNavigationBarLoading();//显示加载动画
			this.page++;
			
			util.request(api.businessOrderList+'?page='+this.page+'&size=20', {
				status:this.active
			}, 'POST').then(res => {
				console.log(res)
				if(res.error==0){
					if(this.page>1){
						 if(res.data.data.length==0){
						 	if(this.page>1){
						 		uni.hideNavigationBarLoading();//关闭加载动画
								this.dx=true
							}
						}else{
							this.orderList = [...this.orderList,...res.data.data]
						}
						
					}
					uni.hideNavigationBarLoading();//关闭加载动画
					
				}
				
				
				
			})
		},
		onShow() {
			
			console.log( uni.getStorageSync('userInfo'))
			this.salesmanNo = uni.getStorageSync('userInfo').salesmanNo
			// 判断是否是业务员
			if(this.identity == 2){
				console.log('---------------------------')
				this.getOrderResume()
			
			}
			this.page=1
			this.getOrderList()
			
			// 请求消息列表
			this.getInfo();
		
		},
		methods: {
			// 获取业务员概要
			getOrderResume(){
				
				util.request(api.orderResume+'?userId='+uni.getStorageSync('userInfo').userId,{},'GET').then(res => {
					this.orderResume = res.data
					console.log(res)
					
				})
			},
			// 获取维度消息
			getInfo() {
				var _that = this;
				util.request(api.messageResume, {}, ).then((res) => {
					//console.log('消息中心',JSON.stringify(res));
					if (res.errno === 0) {
						var num = 0;
						var data = res.data;
						for(var i=0;i<data.length;i++){
							num=num+parseInt(data[i].nums)
						}
						_that.messagenumber= num
					}
				});
			},
			skipToMessage(){
				console.log("消息");
				uni.navigateTo({
					url:"/pages/message/message"
				})
			},
			// 升级
			 upgrade(){
				  console.log(this.constraint)
				  if(this.constraint){
						plus.runtime.openURL(this.downloadUrl);  
				  }
			},
				  // 关闭升级
				  sjClose(){ 
					   this.sj = false
				  },
			// 子组件修改父组件的值
			pages(res){
				console.log('子组件返回的值')
				console.log(res)
				this.page = 1
			},
			table(index){
				this.active = index;
				this.dx = false;
				this.page=1;
				this.getOrderList()
			},
			search(){
				uni.navigateTo({
					url:"/pages/search/search"
				})
			},
			jump(tit,index){
				uni.navigateTo({
					url:"/pages/paymentList/paymentList?tit="+tit+"&index="+index
				})
			},
			personal(){
				uni.navigateTo({
					url:"/pages/personalCenter/personalCenter"
				})
			},
			// 扫码
			scanCode(){
				uni.scanCode({
					 onlyFromCamera: true,
					 success:(res)=>{
						 console.log('条码类型：' + res.scanType);
						 console.log('条码内容：' + res.result);
						 
						 var temp2 = res.result.split('=')[1];
						 console.log(temp2)
						 uni.navigateTo({
						 	url:"/pages/orderDetails/orderDetails?orderId="+temp2+'&userId='+uni.getStorageSync('userInfo').userId
						 })
						 // uni.navigateTo({
						 // 	url:"/pages/paymentList/paymentList?tit="+tit+"&index="+index
						 // })
					 }
				}) 
			
			},
			getOrderList(){
				// 获取订单列表
				console.log(this.page)
				console.log(this.active)
				console.log(uni.getStorageSync('userInfo').userId)
				util.request(api.businessOrderList+'?page='+this.page+'&size=20', {
					status:this.active
				}, 'POST').then(res => {
					console.log(res)
					if(res.error==0){
						this.orderList = res.data.data
						
						
					}
					
					
					uni.stopPullDownRefresh()
				})
			}
		}
	}
</script>

<style>
@import url("./index.css");
</style>
